package cn.xx.product.controller;

import cn.xx.basic.util.AjaxResult;
import cn.xx.basic.util.PageList;
import cn.xx.org.domain.Department;
import cn.xx.org.query.DepartmentQuery;
import cn.xx.product.domain.Product;
import cn.xx.product.mapper.ProductMapper;
import cn.xx.product.query.ProductQuery;
import cn.xx.product.service.IProductService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;

    @Autowired
    private ProductMapper productMapper;

    //分页查询接口
    @PostMapping
    @ApiOperation(value = "分页查询",notes = "")
    public PageList<Product> pageQuery(@RequestBody ProductQuery productQuery) {
        return productService.pageQuery(productQuery);
    }

    //分页列表展示接口
    @PostMapping("/list")
    @ApiOperation(value = "分页查询",notes = "")
    public PageList<Product> pageQueryList(@RequestBody ProductQuery productQuery) {
        productQuery.setState(1);
        return productService.pageQuery(productQuery);
    }

    //单个商品详情展示
    @GetMapping("/{id}")
    public AjaxResult pageQueryOne(@PathVariable("id") Long id) {
        Product product = productMapper.findById(id);
        return AjaxResult.me().setResultObject(product);
    }

    // 修改或保存
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Product product) {
        try {
            if (product.getId() == null) {
                productService.save(product);
            } else {
                productService.update(product);
            }
            return AjaxResult.me().setSuccess(true).setMsg("操作完成了哦");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败").setResultObject("原因:" + e.getMessage());
        }
    }

    // 批量上架
    @PutMapping("/onSale")
    public AjaxResult onSale(@RequestBody Long[] ids ) {
        try {
            productService.onSale(ids);
            return AjaxResult.me().setSuccess(true).setMsg("操作完成了哦");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败").setResultObject("原因:" + e.getMessage());
        }
    }


    // 批量下架
    @PutMapping("/offSale")
    public AjaxResult offSale(@RequestBody Long[] ids ) {
        try {
            productService.offSale(ids);
            return AjaxResult.me().setSuccess(true).setMsg("操作完成了哦");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败").setResultObject("原因:" + e.getMessage());
        }
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除",notes = "根据id删除")
    //删除接口：Delete localhost:8080/department/1
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            productService.delete(id);
            return AjaxResult.me().setSuccess(true).setMsg("操作完成了哦");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败").setResultObject("原因:" + e.getMessage());
        }
    }


    // 批量删除
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids ) {
        try {
            productService.patchDel(ids);
            return AjaxResult.me().setSuccess(true).setMsg("操作完成了哦");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败").setResultObject("原因:" + e.getMessage());
        }
    }

}
